package com.example.sihookuiklydemo.utils


class InPutUtils {
    companion object {
        const val INPUT_VALID = 0
        const val INPUT_EMPTY = 1
        const val INPUT_LENGTH_ERROR = 2
        const val INPUT_FORMAT_WRONG = 3
    }


}

fun checkInput(inputText: String): Int {
    var errorCode = InPutUtils.INPUT_VALID
    errorCode = when (inputText.isNullOrEmpty()) {
        true -> InPutUtils.INPUT_EMPTY
        else -> {
            when (isValidName(inputText)) {
                true -> {
                    var length = inputText.length
                    when (length > 15) {
                        true -> InPutUtils.INPUT_LENGTH_ERROR
                        else -> {
                            InPutUtils.INPUT_VALID
                        }
                    }
                }

                else -> {
                    InPutUtils.INPUT_FORMAT_WRONG
                }
            }

        }
    }
    return errorCode
}

//海外新版只检查字符长度
fun checkNickNameInput(inputText: String): Int {
    var errorCode = InPutUtils.INPUT_VALID
    errorCode = when (inputText.isNullOrEmpty()) {
        true -> InPutUtils.INPUT_EMPTY
        else -> {

            var length = inputText.length
            when (length > 15) {
                true -> InPutUtils.INPUT_LENGTH_ERROR
                else -> {
                    InPutUtils.INPUT_VALID
                }
            }


        }

    }

    return errorCode
}

fun isValidName(name: String): Boolean {
   // val regex = Regex("^[\u4E00-\u9FA5A-Za-z0-9_]+$")

    return !containsEmojis(name)

}
fun containsEmojis(input: String): Boolean {
    val emojiRegex = Regex(
        "[\uD83C\uDF00-\uD83C\uDFFF]|" +
                "[\uD83D\uDC00-\uD83D\uDDFF]|" +
                "[\uD83E\uDD00-\uD83E\uDDFF]|" +
                "[\uD83C\uDDE6-\uD83C\uDDFF]{2}|" +
                "[\uD83C\uDD70-\uD83C\uDD71]|" +
                "[\uD83C\uDD7E-\uD83C\uDD7F]|" +
                "[\uD83C\uDE00-\uD83C\uDE1F]|" +
                "[\uD83C\uDE2F]|" +
                "[\uD83D\uDE00-\uD83D\uDE4F]|" +
                "[\uD83D\uDE80-\uD83D\uDEFF]|" +
                "[\u0023-\u0039]\uFE0F\u20E3"
    )

    return emojiRegex.containsMatchIn(input)
}
fun isValidSn(name: String): Boolean {
    val regex = Regex("^[A-Za-z0-9]+$")
    return regex.matches(name)
}